var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/functionalComponent/table/child/check.tsx
var check_exports = {};
__export(check_exports, {
  AllCheckBox: () => AllCheckBox,
  RowCheckBox: () => RowCheckBox,
  loopChildren: () => loopChildren
});
module.exports = __toCommonJS(check_exports);
var import_antd = require("antd");
var import_react = __toESM(require("react"));
var import_common = require("../base/common");
var import_util = require("../util");
var updateState = [import_common.UpdateFlagEnum.ForceUpdate, import_common.UpdateFlagEnum.ColumnChecked];
function isDisabled({ checkDisabled, row, dataIndex, table }) {
  if ([import_common.CellTypeEnum.AggregateCell, import_common.CellTypeEnum.ExpandRowCell].includes(row.cellType)) {
    return true;
  }
  return import_util.util.isFunction(checkDisabled) ? checkDisabled({ row, dataIndex }) : !!table.isRowSelectionDisabled(row);
}
function loopChildren(table, row, dataIndex, checked, rowIndex, loop = false, updateRows = [], checkDisabled = void 0) {
  var _a;
  if (!row) {
    return false;
  }
  const children = row.children;
  const defaultChecked = !!row[dataIndex];
  if (defaultChecked !== checked) {
    if (!isDisabled({ checkDisabled, row, dataIndex, table })) {
      row[dataIndex] = checked;
      row.__update__ = { rowIndex, dataIndex };
      updateRows.push(row);
    }
  }
  const needLoop = loop || ((_a = table.props.rowSelection) == null ? void 0 : _a.autoCheckedChildren) !== false;
  if (needLoop && children) {
    for (let i = 0, len = children.length; i < len; i++) {
      loopChildren(table, children[i], dataIndex, checked, rowIndex, needLoop, updateRows, checkDisabled);
    }
  }
  return !!row.__update__;
}
var AllCheckBox = import_react.default.memo(
  ({ table, dataIndex: checkedDataIndex, checkDisabled, children }) => {
    const dataSource = (0, import_react.useContext)(import_common.DsContext);
    const dataIndex = checkedDataIndex || table.getCheckBoxDataIndex();
    const [state, setState] = (0, import_react.useState)({ checked: false, indeterminate: false });
    const onCheckedHandler = (0, import_util.useRefCallback)((e) => {
      const checked = e.target.checked;
      const checkedData = [];
      const updateRows = [];
      setState({ checked, indeterminate: false });
      for (let i = 0, len = dataSource.length; i < len; i++) {
        const row = dataSource[i];
        if (loopChildren(table, row, dataIndex, checked, i, false, [], checkDisabled)) {
          updateRows.push(row);
        }
        if (row[dataIndex]) {
          checkedData.push(row);
        }
      }
      table.onCheckedChange(checkedData, updateRows, checked);
    });
    (0, import_util.useIUPEffect)(() => {
      setState(() => {
        let [checkedSize, disabledSize] = [0, 0];
        const dataSize = dataSource.length;
        dataSource.forEach((r) => {
          if (r[dataIndex]) {
            checkedSize++;
          } else if (isDisabled({ checkDisabled, row: r, dataIndex, table })) {
            disabledSize++;
          }
        });
        const calcSize = checkedSize + disabledSize;
        return {
          checked: dataSize === calcSize && dataSize > 0,
          indeterminate: checkedSize > 0 && calcSize < dataSize
        };
      });
    }, [dataSource]);
    return /* @__PURE__ */ import_react.default.createElement(
      import_antd.Checkbox,
      {
        ...state,
        onClick: import_util.stopPropagation,
        style: { margin: "auto" },
        onChange: onCheckedHandler,
        children
      }
    );
  },
  (p, n) => {
    if (updateState.includes(n.table.updateFlag)) {
      return false;
    }
    return (0, import_common.areEqual)(p, n);
  }
);
var RowCheckBox = import_react.default.memo(
  ({ table, row, dataIndex, rowIndex, checkDisabled }) => {
    var _a;
    const [checked, setChecked] = (0, import_react.useState)(row[dataIndex]);
    const checkedHandler = (0, import_util.useRefCallback)((checked2) => {
      if (checked2 === row[dataIndex]) {
        return;
      }
      const { dataSource } = table.state;
      loopChildren(table, row, dataIndex, checked2, rowIndex, false, [], checkDisabled);
      const checkedData = dataSource.filter((r) => r[dataIndex] === true);
      table.onCheckedChange(checkedData, row, checked2);
    });
    (0, import_util.useIUPEffect)(() => {
      if (checked !== row[dataIndex]) {
        setChecked(row[dataIndex]);
      }
    }, [row, row[dataIndex]]);
    (0, import_react.useLayoutEffect)(() => {
      checkedHandler(checked);
    }, [checked]);
    const disabled = isDisabled({ checkDisabled, row, dataIndex, table });
    const rowSelectionType = (_a = table.props.rowSelection) == null ? void 0 : _a.type;
    if (disabled) {
      return checkDisabled ? /* @__PURE__ */ import_react.default.createElement("div", { className: "cover-cell" }, /* @__PURE__ */ import_react.default.createElement(import_antd.Checkbox, { disabled: true, className: "cover-cell", checked })) : null;
    }
    const clickHandler = (e) => {
      (0, import_util.stopPropagation)(e);
      if (table.props.rowSelected && table.props.checkboxSelected && rowSelectionType !== import_common.TableSelectionModel.MULTIPLE_INTERVAL) {
        table.setHighlight(rowIndex, { ctrlKey: false, shiftKey: false });
      }
    };
    const onCheckClick = (e) => {
      setChecked(e.target.checked);
    };
    return /* @__PURE__ */ import_react.default.createElement("div", { className: "cover-cell", onClick: clickHandler }, /* @__PURE__ */ import_react.default.createElement(import_antd.Checkbox, { className: "cover-cell", onChange: onCheckClick, checked }));
  },
  (p, n) => {
    if (n.row.__update__ || updateState.includes(n.table.updateFlag)) {
      return false;
    }
    return (0, import_common.areEqual)(p, n);
  }
);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  AllCheckBox,
  RowCheckBox,
  loopChildren
});
